package com.wfg.blog.contorller;

import com.wfg.blog.utils.QiniuUtils;
import com.wfg.blog.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("upload")
//上传图片
public class UploadController {

    @Autowired
    private QiniuUtils qiniuUtils;


    @PostMapping
    public Result upload(@RequestParam("image") MultipartFile file) {
        //原始文件名称 aa.png
        String originalFilename = file.getOriginalFilename();
        //唯一的图片名称
        String fileName = UUID.randomUUID().toString()+"."+ StringUtils.substringAfterLast(originalFilename,".");
        //上传文件  七牛云
        boolean upload = qiniuUtils.upload(file, fileName);
        if (upload){
            return Result.success(QiniuUtils.url+fileName);
        }
        return Result.fail(20001,"上传失败");
    }

}



